<?php
    
    require_once ("../src/egallery/net/mysql/MysqlConfig.php") ;
    require_once ("../src/egallery/net/mysql/Tables.php") ;
    require_once ("../src/egallery/net/mysql/MysqlConnector.php") ;
    require_once ("../src/egallery/vo/PictureVO.php") ;

    /**
     * This AMFPhp remoting service is used to return the list of all pictures in a gallery.
     * @author eKameleon
     */
    class Gallery
    {
       
        /**
         * Creates a new Gallery instance.
         */
        function Gallery() 
        {
            $mysqlConfig       = new MysqlConfig() ;
            $this->connect     = new MysqlConnector( $mysqlConfig->host , $mysqlConfig->user , $mysqlConfig->pass , $mysqlConfig->name ) ;
            
            $this->methodTable = array 
            (
                
                "getGallery" => array
                (
                    "description" => "generate a list of pictures" ,
                    "access"      => "remote"
                )
                ,
                "_getPicture" => array
                (
                    "description" => "Return a PictureVO." ,
                    "access"      => "private"
                )
                ,
                "_getResult" => array
                (
                    "description" => "Return the result of a MYSQL query." ,
                    "access"      => "private"
                )
                
            ) ;
        } 
        
        /**
          * The internal MysqlConnector instance.
         */
        var $connect ;
        
        /**
         * The method table use by AMFPHP.
         */
        var $methodTable ;
        
        /**
         * This method return the list of all PictureVO in the db.
         */
        function getGallery()
        {
            $stack = array() ;
            
            $query = "SELECT * FROM ". _TABLE_GALLERY_ ;
            
            $result = $this->_getResult( $query ) ;
            
            if (mysql_num_rows($result) > 0)
            {
                while( $row = mysql_fetch_array( $result ) )
                {    
                    
                    $pic = $this->_getPicture( $row ) ;
                    array_push( $stack, $pic ) ;
                    
                }
            }
            
            return $stack ;
            
        }
        
        /**
         * Returns a new PictureVO specified by the row value in argument.
         * @return a new PictureVO specified by the row value in argument.
         */
        function _getPicture( $row )
        {
                                
            $pic = new PictureVO() ;
                    
            $pic->id              = $row["id"] ;
            $pic->description     = utf8_encode( $row["description"] ) ;
            $pic->title           = utf8_encode( $row["title"] ) ;
            $pic->url            = $row["url"] ;
            
            return $pic ;
            
        }
        
        /**
         * Returns the result of a query.
         * @return the result of a query.
         */
        function _getResult( $query )
        {
           return $this->connect->getResult($query) ; 
        }
        
    }
?>
